package sys;

public class SysClock {
	public static float posUpdateTimer = SysParam.POSITION_CHANGE_RATE;
	public static float blockChangeTimer = SysParam.BLOCK_CHANGE_RATE;

	public final static long  globalTime = getTime();
	public static Timer updaterTimer = new Timer(SysParam.SKIP_TICKS,SysParam.SKIP_FRAME_MAX);

	public static void reset() {
		updaterTimer.resetNextTick();
	}

	public static long getTime() {
		return System.nanoTime() / 1000000;
	}

	public static long getDelta() {
		return (getTime() - globalTime);
	}
	
	public static class Timer {
		public Timer(int skipTicks, int skipFrameMax) {
			this.nextTick = getTime();
			this.loopCounter = 0;
			this.skipTicks = skipTicks;
			this.skipFrameMax = skipFrameMax;
		}

		long nextTick;
		int loopCounter;
		int skipTicks;
		int skipFrameMax;

		public void resetNextTick() {
			nextTick = getTime();
		}

		public void resetLoopCounter() {
			loopCounter = 0;
		}

		public boolean isOKForNextLoop() {
			return (getTime() >= nextTick && loopCounter < skipFrameMax);
		}

		public void tick() {
			nextTick += SysParam.SKIP_TICKS;
			loopCounter++;
		}

		public float getInterpolation() {
			return Float.valueOf(getTime() + skipTicks - nextTick) / Float.valueOf(skipTicks);
		}

		public long getNextTick() {
			return nextTick;
		}

		public int getLoopCounter() {
			return loopCounter;
		}
	}
}
